﻿/*
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * See product pages for more information.
 * http://closesudoku.codeplex.com
 * Copyright (C), 2011, hrondra
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

using Fluid.Controls;
using Close.Classes;

namespace Close.controls.FFluid
{

   public class CheckBox : FluidButton
   {
      static Font bigFont = new Font(FontFamily.GenericSansSerif, 10f, FontStyle.Bold);
      static Font smallFont = new Font(FontFamily.GenericSansSerif, 7f, FontStyle.Regular);

      private bool _isChecked = false;
      public bool Checked 
      {
         get { return _isChecked; }
         set { 
               _isChecked = value;
               this.Invalidate();
             }
      }

      protected override void PaintText(FluidPaintEventArgs e)
      {
         FluidButton btn = this;

         string text = btn.Text;
         if (text.Length > 0)
         {
            Brush brush = new SolidBrush(ThemeClass.Settings.NumpadForeColor);
            string bigText = text;
            string smallText = String.Empty;
            Graphics g = e.Graphics;
            Rectangle r = e.ControlBounds;

            int size = r.Height - 5;

            Pen p = new Pen(ThemeClass.Settings.NumpadForeColor);
            g.DrawRectangle(p, new Rectangle(r.X + 2, r.Y + 2, size, size));
            if (_isChecked)
            {
               g.FillRectangle(brush, new Rectangle(r.X + 2 + 2, r.Y + 2 + 2, size - 2 - 1, size - 2 -1));
            }
            //g.DrawLine (p, 0, 0, r.Width, r.Height);
            if (btn.IsDown)
            {
               Pen p2 = new Pen(ThemeClass.Settings.NumpadForeColor);
               g.DrawRectangle(p2, r.X, r.Y, r.Width - 1, r.Height - 1);
            }

            //add enough space to checkbox image
            r.X += size + 2;
            r.Width -= (size + 2);
            
            stringFormat.Alignment = StringAlignment.Near;
            stringFormat.LineAlignment = StringAlignment.Center;
            SizeF bigSize = g.MeasureString(bigText, bigFont);
            float smallWidth = string.IsNullOrEmpty(smallText) ? 0f : e.ScaleFactor.Width * 5f + g.MeasureString(smallText, smallFont).Width;
            int dw = (int)((btn.Width - smallWidth) / 2f);
            //int dh = (int)((btn.Height - bigSize.Height) / 2f);
            int dh = (int)(2f * e.ScaleFactor.Width);
            //r.Inflate(-dw, -dh);
            RectangleF rf = RectFFromRect(r);

            g.DrawString(bigText, bigFont, brush, rf, stringFormat);

            if (!string.IsNullOrEmpty(smallText))
            {
               float width = bigSize.Width + 4f * e.ScaleFactor.Width;
               rf.X += width;
               g.DrawString(smallText, smallFont, brush, rf, stringFormat);
            }
         }
      }
      protected override void PaintButtonBackground(FluidPaintEventArgs e)
      {       
      }

      public override void PerformClick()
      {
         Checked = !_isChecked;
         base.PerformClick();
      }

   }
}
